﻿using Aker.DevTeam;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using Extensions;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace Aker.Hammadde
{
    public partial class Renk : XForm
    {
        int Type = 1;

        public Renk(int type)
        {
            InitializeComponent();
            this.Type = type;
        }

        private void Renk_Load(object sender, EventArgs e)
        {
            if (Type == 1)
                gv.Init(Data.DB.IplikRenk);
            else if (Type == 2)
                gv.Init(Data.DB.ElyafRenk);

            if (!AppSession.UserInfo.Yonetici)
            {
                gv.MakeReadOnly();
                gv.MakeRowSelectable();
            }
            else
                gv.MakeEditable("RenkId");

            gv.OptionsView.ShowViewCaption = false;

            this.txtKod.TextChanged += txt_TextChanged;
            this.txtRenk.TextChanged += txt_TextChanged;
        }


        public int RenkId;
        internal DialogResult Focus(int renkId)
        {

            txtKod.Text = "";
            txtRenk.Text = "";

            if (Type == 1)
            {
                AkerDB.IplikRenkRow ip = Data.DB.IplikRenk.FindByRenkId(renkId);

                if (renkId != -1)
                {
                    if (!ip.IsKodNull())
                        txtKod.Text = ip.Kod;

                    if (!ip.IsRenkNull())
                        txtRenk.Text = ip.Renk;
                }
            }
            else if (Type == 2)
            {
                AkerDB.ElyafRenkRow elyaf = Data.DB.ElyafRenk.FindByRenkId(renkId);

                if (renkId != -1)
                {
                    if (!elyaf.IsKodNull())
                        txtKod.Text = elyaf.Kod;

                    if (!elyaf.IsRenkNull())
                        txtRenk.Text = elyaf.Renk;
                }
            }

            btnEkle.Focus();
            return this.ShowDialog();
        }

        private void btnEkle_Click(object sender, EventArgs e)
        {
            string filter = "Kod = null and Renk = null";
            if (txtKod.Text != "")
                filter = "Kod = '" + txtKod.Text + "' and ";
            else
                filter = "Kod is null and ";

            if (txtRenk.Text != "")
                filter += "Renk = '" + txtRenk.Text + "'";
            else
                filter += "Renk is null";



            if (Type == 1)
            {
                System.Data.DataRow[] drs = Data.DB.IplikRenk.Select(filter);
                if (drs.Length == 0)
                {
                    AkerDB.IplikRenkRow ip = Data.DB.IplikRenk.NewIplikRenkRow(); // AddIplikRenkRow(txtKod.Text.CamelCasing(), txtRenk.Text.CamelCasing());
                    if (txtKod.Text == "")
                        ip.SetKodNull();
                    else
                        ip.Kod = txtKod.Text.CamelCasing();

                    if (txtRenk.Text == "")
                        ip.SetRenkNull();
                    else
                        ip.Renk = txtRenk.Text.CamelCasing();

                    Data.DB.IplikRenk.Rows.Add(ip);
                    Data.TAM.IplikRenkTableAdapter.Update(Data.DB.IplikRenk);
                    RenkId = ip.RenkId;

                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                }
                else if (drs.Length == 1)
                {
                    RenkId = gv.GetRowCellValue(0, "RenkId").ToIntNN();
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                }

                else
                    DevExpress.XtraEditors.XtraMessageBox.Show("Yeni eklemek için listede aynısından olmamalı! Listeden çift tıklayarak seçebilirsiniz.", "Uyarı", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

            else if (Type == 2)
            {
                System.Data.DataRow[] drs = Data.DB.ElyafRenk.Select(filter);
                if (drs.Length == 0)
                {
                    AkerDB.ElyafRenkRow elyaf = Data.DB.ElyafRenk.NewElyafRenkRow(); // AddElyafRenkRow(txtKod.Text.CamelCasing(), txtRenk.Text.CamelCasing());
                    if (txtKod.Text == "")
                        elyaf.SetKodNull();
                    else
                        elyaf.Kod = txtKod.Text.CamelCasing();

                    if (txtRenk.Text == "")
                        elyaf.SetRenkNull();
                    else
                        elyaf.Renk = txtRenk.Text.CamelCasing();

                    Data.DB.ElyafRenk.Rows.Add(elyaf);
                    Data.TAM.ElyafRenkTableAdapter.Update(Data.DB.ElyafRenk);
                    RenkId = elyaf.RenkId;

                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                }
                else if (drs.Length == 1)
                {
                    RenkId = gv.GetRowCellValue(0, "RenkId").ToIntNN();
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                }

                else
                    DevExpress.XtraEditors.XtraMessageBox.Show("Yeni eklemek için listede aynısından olmamalı! Listeden çift tıklayarak seçebilirsiniz.", "Uyarı", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
        }

        private void txt_TextChanged(object sender, EventArgs e)
        {
            this.txtKod.TextChanged -= txt_TextChanged;
            this.txtRenk.TextChanged -= txt_TextChanged;

            txtKod.Text = txtKod.Text.CamelCasing();
            txtRenk.Text = txtRenk.Text.CamelCasing();

            SetFilter();

            this.txtKod.TextChanged += txt_TextChanged;
            this.txtRenk.TextChanged += txt_TextChanged;
        }

        private void gc_DoubleClick(object sender, EventArgs e)
        {
            Point pt = gc.PointToClient(Control.MousePosition);
            GridHitInfo info = gv.CalcHitInfo(pt);
            if (info.RowHandle > -1)
            {
                RenkId = gv.GetFocusedRowCellValue("RenkId").ToIntNN();

                if (!AppSession.UserInfo.Yonetici || (AppSession.UserInfo.Yonetici && info.Column.FieldName == "RenkId"))
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
        }

        private void Renk_Shown(object sender, EventArgs e)
        {
            SetFilter();

            txtKod.Focus();
        }

        private void SetFilter()
        {
            List<string> filters = new List<string>();
            if (txtKod.Text != "")
                filters.Add("StartsWith([Kod], '" + txtKod.Text.CamelCasing() + "')");

            if (txtRenk.Text != "")
                filters.Add("StartsWith([Renk], '" + txtRenk.Text.CamelCasing() + "')");

            gv.ActiveFilterString = string.Join(" and ", filters);
        }

        private void gv_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            gv.EndEditing();
            if (Type == 1)
                Data.TAM.IplikRenkTableAdapter.Update(Data.DB.IplikRenk);
            else if (Type == 2)
                Data.TAM.ElyafRenkTableAdapter.Update(Data.DB.ElyafRenk);
        }
    }
}